package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author
 */
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;
    @Autowired
    private CheckItemService  checkItemService;

    /**
     * 检查组管理分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result pageQuery(QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }


    /**
     * 检查组新建
     */
    @PostMapping("/add")
    public   Result  add(@RequestBody CheckGroup  checkGroup,  Integer[] checkitemIds){

        checkGroupService.add(checkGroup,checkitemIds);
        return  new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据id查找检查组
     */

    @GetMapping("/findById")
    public   Result  findById( Integer  id){
        CheckGroup  checkGroup =   checkGroupService.findById(id);
        return  new Result(true,MessageConst.ACTION_SUCCESS,checkGroup);
    }

    /**
     * 查询中间表的id
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public   Result  selectById( Integer id){
        List<Integer>  idList = checkGroupService.selectById(id);

        return  new Result(true,MessageConst.ACTION_SUCCESS,idList);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @PostMapping("/edit")
    public   Result  update(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        checkGroupService.update(checkGroup,checkitemIds);
        return  new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @PostMapping("/deleteCheckGroupitemById")
    public   Result  deleteById(  Integer  id){
        checkGroupService.deleteById(id);
        return  new Result(true,MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    @GetMapping("/findAll")
    public   Result  findAll(){
      List<CheckGroup>  checkGroupList = checkGroupService.findAll();

        return  new Result(true,MessageConst.ACTION_SUCCESS,checkGroupList);
    }
}
